package mytankGame02;

import java.util.Vector;

public class enemyTank extends tank implements Runnable {
    final int MAX_SHOT_NUM = 3;
    public Vector<Shot> Shots = new Vector<>();
    public static Vector<enemyTank> enemyTanks = new Vector<>();

    public void setEnemyTanks(Vector<enemyTank> enemyTanks) {
        enemyTanks = enemyTanks;
    }

    public boolean isTouched() {
        switch (getDirection()) {
            case 0:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    enemyTank tank = enemyTanks.get(i);
                    if (this == tank) {
                        return false;
                    }
                    //左上角判断
                    if (tank.getDirection() == 0 || tank.getDirection() == 2) {
                        if (getX() > tank.getX() && getX() < tank.getX() + 40
                                && getY() > tank.getY() && getY() < tank.getY() + 60) {
                            is_touched = true;
                        }
                        //右上角判断
                        if (getX() + 40 > tank.getX() && getX() + 40 < tank.getX() + 40
                                && getY() > tank.getY() && getY() < tank.getY() + 60) {
                            is_touched = true;
                        }
                    } else {
                        if (getX() > tank.getX() && getX() < tank.getX() + 60
                                && getY() > tank.getY() && getY() < tank.getY() + 40) {
                            is_touched = true;
                        }
                        //右上角判断
                        if (getX() + 40 > tank.getX() && getX() + 40 < tank.getX() + 60
                                && getY() > tank.getY() && getY() < tank.getY() + 40) {
                            is_touched = true;
                        }
                    }
                }
                break;
            case 1:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    enemyTank tank = enemyTanks.get(i);
                    if (this == tank) {
                        return false;
                    }
                    //左上角判断
                    if (tank.getDirection() == 0 || tank.getDirection() == 2) {
                        if (getX() + 60 > tank.getX() && getX() + 60 < tank.getX() + 40
                                && getY() > tank.getY() && getY() < tank.getY() + 60) {
                            is_touched = true;
                        }
                        //右上角判断
                        if (getX() + 60 > tank.getX() && getX() + 60 < tank.getX() + 40
                                && getY() + 40 > tank.getY() && getY() + 40 < tank.getY() + 60) {
                            is_touched = true;
                        }
                    } else {
                        if (getX() + 60 > tank.getX() && getX() + 60 < tank.getX() + 60
                                && getY() > tank.getY() && getY() < tank.getY() + 40) {
                            is_touched = true;
                        }
                        //右上角判断
                        if (getX() + 60 > tank.getX() && getX() + 60 < tank.getX() + 60
                                && getY() + 40 > tank.getY() && getY() + 40 < tank.getY() + 40) {
                            is_touched = true;
                        }
                    }
                }
                break;
            case 2:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    enemyTank tank = enemyTanks.get(i);
                    if (this == tank) {
                        return false;
                    }
                    //左上角判断
                    if (tank.getDirection() == 0 || tank.getDirection() == 2) {
                        if (getX() > tank.getX() && getX() < tank.getX() + 40
                                && getY() + 60 > tank.getY() && getY() + 60 < tank.getY() + 60) {
                            is_touched = true;
                        }
                        //右上角判断
                        if (getX() > tank.getX() && getX() < tank.getX() + 40
                                && getY() + 60 > tank.getY() && getY() + 60 < tank.getY() + 60) {
                            is_touched = true;
                        }
                    } else {
                        if (getX() + 40 > tank.getX() && getX() + 40 < tank.getX() + 60
                                && getY() + 60 > tank.getY() && getY() + 60 < tank.getY() + 40) {
                            is_touched = true;
                        }
                        //右上角判断
                        if (getX() + 40 > tank.getX() && getX() + 40 < tank.getX() + 60
                                && getY() + 60 > tank.getY() && getY() + 60 < tank.getY() + 40) {
                            is_touched = true;
                        }
                    }
                }
                break;
            case 3:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    enemyTank tank = enemyTanks.get(i);
                    if (this == tank) {
                        return false;
                    }
                    //左上角判断
                    if (tank.getDirection() == 0 || tank.getDirection() == 2) {
                        if (getX() > tank.getX() && getX() < tank.getX() + 40
                                && getY() > tank.getY() && getY() < tank.getY() + 60) {
                            return true;
                        }
                        //右上角判断
                        if (getX() > tank.getX() && getX() < tank.getX() + 40
                                && getY() > tank.getY() && getY() < tank.getY() + 60) {
                            return true;
                        }
                    } else {
                        if (getX() > tank.getX() && getX() < tank.getX() + 60
                                && getY() > tank.getY() && getY() < tank.getY() + 40) {
                            return true;
                        }
                        //右上角判断
                        if (getX() > tank.getX() && getX() < tank.getX() + 60
                                && getY() + 40 > tank.getY() && getY() + 40 < tank.getY() + 40) {
                            return true;
                        }
                    }
                }
                break;
        }
        return false;
    }

    public enemyTank(int x, int y, int direction) {
        super(x, y, direction);
    }

    @Override
    public void run() {
        Shot shot = null;
        while (true) {
            if (getIs_alive() && Shots.size() < 5) {
                switch (getDirection()) {
                    case 0:
                        shot = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        shot = new Shot(getX() + 60, getY() + 20, 1);
                        break;
                    case 2:
                        shot = new Shot(getX() + 20, getY() + 60, 2);
                        break;
                    case 3:
                        shot = new Shot(getX(), getY() + 20, 3);
                        break;
                }
            }
            Shots.add(shot);
            new Thread(shot).start();
            switch (getDirection()) {
                case 0:
                    for (int i = 0; i < 30; i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        if (getY() > 0 && !isTouched()) {
                            moveUp();
                        }
                    }

                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        if (getX() + 60 < 1000 && !isTouched()) {
                            moveRight();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        if (getY() + 60 < 750 && !isTouched()) {
                            moveDown();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        if (getX() > 0 && !isTouched()) {
                            moveLeft();
                        }
                    }
                    break;
            }
            setDirection((int) (Math.random() * 4));
            ;
            if (!getIs_alive()) {
                break;
            }
        }

    }
}
